@inline function dd_mm_sss_string(nanosecs::Nanoseconds)
    return dd_mm_sss_string(nanoseconds(nanosecs))
end

const DUR_P = DURATION_CHARS[1];    idx = nextind(DURATION_CHARS,1);
const DUR_YR = DURATION_CHARS[idx]; idx = nextind(DURATION_CHARS,idx);
const DUR_MO = DURATION_CHARS[idx]; idx = nextind(DURATION_CHARS,idx);
const DUR_DY = DURATION_CHARS[idx]; idx = nextind(DURATION_CHARS,idx);
const DUR_HR = DURATION_CHARS[idx]; idx = nextind(DURATION_CHARS,idx);
const DUR_MI = DURATION_CHARS[idx]; idx = nextind(DURATION_CHARS,idx);
const DUR_SC = DURATION_CHARS[idx];

function Base.string(x::TimeSpan)
    ns, mo = value(x)
    ns_sgn = signbit(ns); ns=abs(ns)
    mo_sgn = signbit(mo); mo=abs(mo)
    sgn = mo_sgn == ns_sgn ? (mo_sgn ? "-" : "") : (mo_sgn ? (ns > 0 ? "∓" : "") : (mo > 0 ? "±" : "-"))
    yr, mo = fldmod(mo, MONTHS_PER_YEAR)
    dy, nanos = fldmod(ns, NANOSECONDS_PER_DAY)
    # fldmod ensures monthmonths >= 0
    yearmonthday_str = mo_sgn ? "-" : "+"
    if !(yr==0 && mo==0 && dy==0)
        yearmonthday_str = string(yearmonthday_str, yr, DUR_YR, mo, DUR_MO, dy, DUR_DY)
    end    
    str = ""
    hms_str = ""
    if nanos > 0
        micros, nanos = fldmod(nanos, NANOSECONDS_PER_MICROSECOND)
        millis, micros = fldmod(micros, MICROSECONDS_PER_MILLISECOND)
        scs, millis = fldmod(millis, MILLISECONDS_PER_SECOND)
        mns, scs = fldmod(scs, SECONDS_PER_MINUTE)
        hrs, mns = fldmod(mns, MINUTES_PER_HOUR)
        nano_str =  nanos != 0 ? threedigits(nanos) : ""
        micro_str =  nanos+micros != 0 ? threedigits(micros) : ""
        milli_str =  nanos+micros+millis != 0 ? string(DEC_SEP, threedigits(millis)) : ""
        nano_str = string(milli_str, micro_str, nano_str)                                    
        # hms_str = string(twodigits(hrs),"H",twodigits(mns),"M", twodigits(scs), nano_str,"S")
        hms_str = string(hrs,DUR_HR,mns,DUR_MI, scs, nano_str,DUR_SC)
    end
    daysec_str = dd_mm_sss_string(abs(Nanoseconds(x)))
    if (sgn != "") && (sgn !="-")
       str = string(yearmonthday_str, (ns_sgn ? "-" : "+"), hms_str)
    else 
       str = string(yearmonthday_str, hms_str)
    end
    
    return str
end


function isostring(x::TimeSpan)
    ns, mo = value(x)
    ns_sgn = signbit(ns); ns=abs(ns)
    mo_sgn = signbit(mo); mo=abs(mo)
    sgn = mo_sgn == ns_sgn ? (mo_sgn ? "-" : "") : (mo_sgn ? (ns > 0 ? "∓" : "") : (mo > 0 ? "±" : "-"))
    yr, mo = fldmod(mo, MONTHS_PER_YEAR)
    dy, nanos = fldmod(ns, NANOSECONDS_PER_DAY)
    # fldmod ensures monthmonths >= 0
    if yr==0 && mo==0 && dy==0
        yearmonthday_str = "P"
    else
        yearmonthday_str = string("P", yr, "Y", mo, "M", dy, "D")
    end    
    str = ""
    hms_str = ""
    if nanos > 0
        micros, nanos = fldmod(nanos, NANOSECONDS_PER_MICROSECOND)
        millis, micros = fldmod(micros, MICROSECONDS_PER_MILLISECOND)
        scs, millis = fldmod(millis, MILLISECONDS_PER_SECOND)
        mns, scs = fldmod(scs, SECONDS_PER_MINUTE)
        hrs, mns = fldmod(mns, MINUTES_PER_HOUR)
        nano_str =  nanos != 0 ? threedigits(nanos) : ""
        micro_str =  nanos+micros != 0 ? threedigits(micros) : ""
        milli_str =  nanos+micros+millis != 0 ? string(DEC_SEP, threedigits(millis)) : ""
        nano_str = string(milli_str, micro_str, nano_str)                                    
        # hms_str = string(twodigits(hrs),"H",twodigits(mns),"M", twodigits(scs), nano_str,"S")
        hms_str = string(hrs,"H",mns,"M", scs, nano_str,"S")
    end
    daysec_str = dd_mm_sss_string(abs(Nanoseconds(x)))
    if (sgn != "") && (sgn !="-")
       str = string((mo_sgn ? "-" : ""), yearmonthday_str, (ns_sgn ? "-" : "+"),"P", hms_str)
    else 
       str = string(sgn, yearmonthday_str, hms_str)
    end
    
    return str
end
